<template>
  <div id="customer-departments">
    <div class="card">
      <div class="card-header">
        <nav aria-label="breadcrumb" role="navigation">
          <ol class="breadcrumb pb-0">
            <li class="breadcrumb-item">
              <router-link to="/customers">企业客户</router-link>
            </li>
            <li class="breadcrumb-item active" aria-current="page">部门信息</li>

            <li class="breadcrumb-item ms-auto">
              <router-link to="/passengers">员工信息</router-link>
            </li>
          </ol>
        </nav>
      </div>
      <div class="card-body">
        <div class="row">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="部门名称"
              v-model.trim="searchName"
            />
          </div>
          <div class="col-2">
            <my-select-customer
              :customerId.sync="searchCustomerId"
            ></my-select-customer>
          </div>

          <div class="col-9">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click="search()"
            >
              查找
            </button>

            <button
              type="button"
              class="btn btn-secondary ms-2 btn-sm"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>

            <router-link
              to="/customer/department"
              class="btn btn-success float-end"
              >新建</router-link
            >
          </div>
        </div>
      </div>
    </div>
    <table class="table table-striped table-sm">
      <thead>
        <tr>
          <th>公司</th>
          <th>部门编号</th>
          <th>部门名称</th>
          <th>上级部门</th>
          <th>备注</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <template v-for="(info, index) in dataList">
          <tr :key="index">
            <td>{{ info.customerName }}</td>
            <td>{{ info.departmentNo }}</td>
            <td>{{ info.name }}</td>
            <td>{{ info.parentDepartmentName }}</td>
            <td>{{ info.remark }}</td>
            <td>
              <router-link
                :to="`/customer/department?id=` + info.id"
                class="btn btn-sm btn-primary text-white"
                >修改</router-link
              >
              <router-link
                :to="`/customer/department?id=` + info.id + `&copies=1`"
                class="btn btn-sm btn-secondary text-white ms-3"
                >复制</router-link
              >
              <router-link :to="`/customer/department/` + info.id" class="ms-5"
                >详情</router-link
              >
            </td>
          </tr>
        </template>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>
  </div>
</template>

<script>
import {
  searchDepartments,
  searchCustomers,
  deleteCustomerApprover,
} from "@/api/customer.js";
import MyPagination from "@/components/my-pagination.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";

export default {
  components: {
    MyPagination,
    MySelectCustomer,
  },
  data() {
    return {
      id: 0,
      customerId: 0,
      deptId: 0,
      name: "",
      parentDeptId: 0,
      mobile: "",
      phone: "",
      linkMan: "",
      email: "",
      remark: "",

      dataList: [],
      customers: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      searchName: "",
      searchCustomerId: -1,
    };
  },
  computed: {
    isDataAdmin() {
      return this.$store.getters.isDataAdmin;
    },
  },
  mounted: function () {},
  activated: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      console.log("this.$router.replace('/h5/customer/departments')");
      this.$router.replace("/h5/customer/departments");
    } else {
      this.search();
    }
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      // this.$store.dispatch('showLoading', { 'loadingText': msg })
    },
    hideLoading: function () {
      // this.$store.dispatch('hideLoading')
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.searchName,
        "sc.customerId": this.searchCustomerId,
      };
      searchDepartments(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
      });
    },
    reset: function () {
      this.name = "";
      this.searchName = "";
      this.searchCustomerId = -1;
      this.sc.pageNo = 1;
      this.search();
    },
    searchCustomers: function () {
      if (this.customers.length === 0) {
        searchCustomers(null, (jsonResult) => {
          this.customers = jsonResult.dataList;
        });
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
